import os
import signal
import time 
import libtmux

def killport(port):
    '''root authority is required'''
    try:
        command="kill -9 $(netstat -nlp | grep :"+str(port)+" | awk '{print $7}' | awk -F'/' '{{ print $1 }}')"
        os.system(command)
        print(f"killing {port} is succeed.")
    except:
        pass 
        print(f"{port} no need to kill")
    time.sleep(2)

if __name__ == "__main__":

    ports = [5050, 5051, 5052, 5053, 5054, 5055, 5056, 5057, 5058, 5059, 5060, 5061, 5062, 5063, 5064, 5065]

    print("正在kill进程")
    for p in ports:
        killport(p)

    t = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]
    topp = [0.8, 0.85]
    command_to_serve = [
        f"python server_convo_v2_7b.py --server_port 5050 --gpu_id 0 --t {t[0]} --topp {topp[0]}",
        f"python server_convo_v2_7b.py --server_port 5051 --gpu_id 0 --t {t[1]} --topp {topp[0]}",
        f"python server_convo_v2_7b.py --server_port 5052 --gpu_id 1 --t {t[2]} --topp {topp[0]}",
        f"python server_convo_v2_7b.py --server_port 5053 --gpu_id 1 --t {t[3]} --topp {topp[0]}",
        f"python server_convo_v2_7b.py --server_port 5054 --gpu_id 2 --t {t[4]} --topp {topp[0]}",
        f"python server_convo_v2_7b.py --server_port 5055 --gpu_id 2 --t {t[5]} --topp {topp[0]}",
        f"python server_convo_v2_7b.py --server_port 5056 --gpu_id 3 --t {t[6]} --topp {topp[0]}",
        f"python server_convo_v2_7b.py --server_port 5057 --gpu_id 3 --t {t[7]} --topp {topp[0]}",
        f"python server_convo_v2_7b.py --server_port 5058 --gpu_id 4 --t {t[0]} --topp {topp[1]}",
        f"python server_convo_v2_7b.py --server_port 5059 --gpu_id 4 --t {t[1]} --topp {topp[1]}",
        f"python server_convo_v2_7b.py --server_port 5060 --gpu_id 5 --t {t[2]} --topp {topp[1]}",
        f"python server_convo_v2_7b.py --server_port 5061 --gpu_id 5 --t {t[3]} --topp {topp[1]}",
        f"python server_convo_v2_7b.py --server_port 5062 --gpu_id 6 --t {t[4]} --topp {topp[1]}",
        f"python server_convo_v2_7b.py --server_port 5063 --gpu_id 6 --t {t[5]} --topp {topp[1]}",
        f"python server_convo_v2_7b.py --server_port 5064 --gpu_id 7 --t {t[6]} --topp {topp[1]}",
        f"python server_convo_v2_7b.py --server_port 5065 --gpu_id 7 --t {t[7]} --topp {topp[1]}",
    ]

    t_replace = []
    topp_replace = []
    for i in t:
        t_replace.append(str(i).replace(".", "_"))
    for i in topp:
        topp_replace.append(str(i).replace(".", "_"))
    
    t = t_replace
    topp = topp_replace
    sessions_names = [
        f"t{t[0]}_topp{topp[0]}",
        f"t{t[1]}_topp{topp[0]}",
        f"t{t[2]}_topp{topp[0]}",
        f"t{t[3]}_topp{topp[0]}",
        f"t{t[4]}_topp{topp[0]}",
        f"t{t[5]}_topp{topp[0]}",
        f"t{t[6]}_topp{topp[0]}",
        f"t{t[7]}_topp{topp[0]}",
        f"t{t[0]}_topp{topp[1]}",
        f"t{t[1]}_topp{topp[1]}",
        f"t{t[2]}_topp{topp[1]}",
        f"t{t[3]}_topp{topp[1]}",
        f"t{t[4]}_topp{topp[1]}",
        f"t{t[5]}_topp{topp[1]}",
        f"t{t[6]}_topp{topp[1]}",
        f"t{t[7]}_topp{topp[1]}",
    ]
    server = libtmux.Server()
    print(server.list_sessions())

    all_sessions = []
    for s in server.list_sessions():
        all_sessions.append(str(s)[str(s).index(" ")+1:-1])
    
    print(all_sessions)

    # exit(0)
    index = 0
    for s_name in sessions_names:
        if s_name in all_sessions:
            s = server.find_where({"session_name": s_name})
        else :
            s = server.new_session(s_name)

        print(f"正在启动程序：{s_name}")
        # s.attached_
        pane = s.attached_pane
        # window.select_pane()
        # pane = window.split_window(attach=False)
        pane.send_keys(command_to_serve[index])
        pane.enter()
        time.sleep(1)
        index += 1

